package tech.xs.workflow.bpmn;

import org.dom4j.Element;
import tech.xs.workflow.bpmn.constant.BpmnElementConstant;
import tech.xs.workflow.bpmn.element.event.BaseEventElement;
import tech.xs.workflow.bpmn.element.event.EndEventElement;
import tech.xs.workflow.bpmn.element.event.StartEventElement;

import java.util.ArrayList;
import java.util.List;

public class BpmnEventElementHelper {

    public static List<BaseEventElement> analyze(Element process) {
        List<BaseEventElement> eventList = new ArrayList<>();
        eventList.addAll(analyzeStartEvent(process));
        eventList.addAll(analyzeEndEvent(process));
        return eventList;
    }

    private static List<EndEventElement> analyzeEndEvent(Element process) {
        List<EndEventElement> eventList = new ArrayList<>();
        List<Element> elements = process.elements(BpmnElementConstant.Event.END);
        for (Element element : elements) {
            EndEventElement bpmnEndEvent = new EndEventElement();
            BpmnElementHelper.analyzeBaseNode(element, bpmnEndEvent);
            eventList.add(bpmnEndEvent);
        }
        return eventList;
    }

    private static List<StartEventElement> analyzeStartEvent(Element process) {
        List<StartEventElement> eventList = new ArrayList<>();
        List<Element> elements = process.elements(BpmnElementConstant.Event.START);
        for (Element element : elements) {
            StartEventElement bpmnStartEvent = new StartEventElement();
            BpmnElementHelper.analyzeBaseNode(element, bpmnStartEvent);
            eventList.add(bpmnStartEvent);
        }
        return eventList;
    }

}
